One of the major strengths of BCS is how easy it is
to integrate with almost any external data source. This section
introduces the API and how to use Visual Studio 2010 to create a simple
.NET connector. This connector example connects just to a data file but
can be extended to connect to more complex data sources. .NET connectors
can be discovered through the Discover function in SPD 2010.
1. Creating a Sample Flat File Data Source
First of all, we need a flat file with some data in it. Start by adding a .txt file on this path on the SharePoint server:
C:\Shared\zipcodes.txt
In real-life scenarios, it is
likely to be on a file share, but for this example we keep it simple. In
this example, the file contains city names and zip codes, separated
with a comma and each name/zip pair on a new line. For this example,
Danish zip codes are used. The file should look like this:
2750,Ballerup
1810,Frederiksberg
1720,Copenhagen West
...
2. Creating a .NET Assembly Connector Project in Visual Studio 2010
A .NET Assembly Connector is
created as the project type Business Data Connectivity Model (BDC
Model) in Visual Studio 2010. Open Visual Studio 2010, click File,
select New, and then select Project. Choose the Business Data
Connectivity Model project type, and name the project ZipCodesModel, as
shown in Figure 1. Then click OK.
Specify the SharePoint site where the model should be deployed (Figure 2), and click Finish.
Note that BDCM projects
can be deployed only as a farm solution. This is because the model has
to be deployed to the metadata store.
The BDCM project type creates a number of files automatically. These files shown in Solution Explorer (Figure 3)
are the minimum required to make a new model and deploy it. If the BDCM
is part of a third-party solution, it is likely that an alternative
deployment method is used and the feature files and WSP package files
can be removed.
The required references to the
SharePoint assemblies are automatically added. A feature for enabling
and disabling the BDCM is included, and a WSP package containing the
feature and model is added.
The BDC model itself
contains a definition of the data source, connection information, and
access layer information (query and return type information). The BDC
model contains two classes: a class that defines the entity or external
content type that this BDC model returns, and a class containing the
code used to connect to the data source, query it, and return the
entities it contains.
3. Creating an Entity (External Content Type)
The first real development task
is to create the entity to be returned. It is very important to make
sure that the entity contains the appropriate data to fulfill the
business requirement for this .NET connector. To do this, the Entity1.cs file will be renamed ZipCodesEntity.cs and modified to map to the zip codes file.
To rename the file called Entity1.cs, right-click it in Solution Explorer and select Rename from the context menu. Type in ZipCodesEntity.cs, and click Yes when asked if all references should be renamed.
To modify the mapping, open the entity file by double-clicking ZipCodesEntity.cs. Delete the existing properties, and add two new properties called ZipCode and City. The class should now look as follows:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ZipCodesModel.BdcModel1
{
public partial class ZipCodesEntity
{
public string ZipCode { get; set; }
public string City { get; set; }
}
}
4. Creating an Entity Service Class
The entity service class is
this example is used to query the data source—in this example, the
zipcodes text file. The output of the entity service class will be one
or all objects of the ZipCodesEntity class, depending on the operation performed.
Rename the Entity1Service.cs file to ZipCodesEntityService.cs as with the entity file, and click Yes when asked to update all references.
In Solution Explorer, double-click ZipCodesEntityService.cs to open it in code view.
As the data source is a text file, add the System.IO namespace, which contains the required classes for reading from the zipcodes text file.
using System.IO;
Some methods are required for
the entity service class to function. The entity service class defines
the Finder and Specific Finder methods used to return entities from the
data source (zipcodes text file).
ReadList() is the
Finder method of the BDC model. In this example, it should return all
zipcode/city pairs from the data source. The method returns an
IEnumerable generic collection of entities, which, in this case, is a
collection of ZipCodesEntity objects.
In this example, the objects are just created in memory, but advanced
streaming schemes might be applied if the data source contains larger
data sets. The ReadList() method can be implemented as shown in Listing 1.
Example 1. Implementation of the ReadList Method
public static IEnumerable<ZipCodesEntity> ReadList()
{
List<ZipCodesEntity> zipCodesEntityList = new List<ZipCodesEntity>();
TextReader textReader = new StreamReader(@"C:\Shared\ZipCodes.txt");
string zipCodeEntry;
while ((zipCodeEntry = textReader.ReadLine()) != null)
{
ZipCodesEntity zipCodesEntity = new ZipCodesEntity();
string[] entityData = zipCodeEntry.Split(',');
zipCodesEntity.ZipCode = entityData[0];
zipCodesEntity.City = entityData[1];
zipCodesEntityList.Add(zipCodesEntity);
}
textReader.Close();
return zipCodesEntityList;
}
|
The ReadItem(string zipCode) method defines the Specific Finder method for the BDC model. In this example, it returns a ZipCodesEntity object with the zipcode/city pair matching the zipcode argument. The ReadItem() method can be implemented as shown in Listing 2.
Example 2. Implementation of the ReadItem Method
public static ZipCodesEntity ReadItem(string zipCode)
{
foreach (ZipCodesEntity zipCodesEntity in ReadList())
{
if (zipCodesEntity.ZipCode == zipCode)
return zipCodesEntity;
}
return null;
}
|
It should now be possible
to compile the assembly, which can be considered a .NET Assembly
Connector at this point. Now the BDC model will be created, and the
connection to the relevant data source, query capabilities, and return
value type are defined.